﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using System.IO;
using System.Data;
using App.Core.DB;
using System.Text;

public partial class Pages_Private_ProfileView : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            BindProfileInfo();
        }
    }

    protected void BindPageInfo()
    {
        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }

    protected void BindProfileInfo()
    {
        BtcHousingEntities context = new BtcHousingEntities();
        
        var customer = context.Customers.FirstOrDefault(C => C.ID == SessionCache.CurrentUser.CustomerID);                     

        String table = @"<table cellpadding='3' cellspacing='0' width='100%' class='GridView'>
                                <colgroup>
                                    <col style='width:20%;' />
                                    <col />
                                </colgroup>
                                {0}
                            </table>";
        StringBuilder sb = new StringBuilder();
        sb.AppendFormat("<tr><td class='bold'>Name:</td> <td>{0}</td></tr>", customer.Name.HtmlEncode());
        sb.AppendFormat("<tr><td class='bold'>Email:</td> <td>{0}</td></tr>", SessionCache.CurrentUser.LoginName);
        sb.AppendFormat("<tr><td class='bold'>Address:</td> <td>{0}</td></tr>", customer.Address.HtmlEncode());
        sb.AppendFormat("<tr><td class='bold'>Zip Code:</td> <td>{0}</td></tr>", customer.ZipCode.HtmlEncode());
        sb.AppendFormat("<tr><td class='bold'>Phone:</td> <td>{0}</td></tr>", customer.Phone.HtmlEncode());
        sb.AppendFormat("<tr><td class='bold'>Feedback:</td> <td>{0}</td></tr>", customer.Comment.HtmlEncode());

        ltrProfileDetails.Text = String.Format(table, sb.ToString());

        String intrestedLoan = customer.HasInterestInLoan ? "Yes" : "No";
        String commaSeparatedName = String.Empty;

        UtilityDAO dao = new UtilityDAO();
        App.Data.DbParameter[] parameters = new[] {
            new App.Data.DbParameter("@CustomerID", customer.ID),
            new App.Data.DbParameter("@BusinessTypeID", ConfigReader.DeveloperBusinessTypeID)
            };

        DataSet ds = dao.GetDataSet(AppSQL.GET_PROPERTYTYPE_BY_CUSTOMER, parameters, false);

        if (!ds.IsEmpty())
        {
            DataTable dt = ds.Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow row = dt.Rows[i];
                String name = NullHandler.GetString(row["Name"]);
                if (i > 0 )
                    commaSeparatedName = commaSeparatedName + ", " + name;
                else
                    commaSeparatedName = name;
            }

        }
        //
        UtilityDAO dao2 = new UtilityDAO();
        App.Data.DbParameter[] parametersZones = new[] {            
            new App.Data.DbParameter("@CustomerID", customer.ID)
            };

        DataSet dsZones = dao2.GetDataSet(AppSQL.GET_ZONES_BY_CUSTOMER, parametersZones, false);
        String commaSeparatedZones = String.Empty;
        
        int _CityID = 0;
        if (!dsZones.IsEmpty())
        {
            DataTable dtZones = dsZones.Tables[0];
            
            for (int i = 0; i < dtZones.Rows.Count; i++)
            {
                DataRow row = dtZones.Rows[i];
                String name = NullHandler.GetString(row["Name"]);
                _CityID = NullHandler.GetInt32(row["CityID"]);
                if (i > 0)
                    commaSeparatedZones = commaSeparatedZones + ", " + name;
                else
                {
                    //int _CityId = Convert.ToInt32(row[2]); 
                    commaSeparatedZones = name;                    
                }
            }

        }
        var cities = context.Cities.FirstOrDefault(C => C.ID == _CityID);
        String table2 = @"<table cellpadding='3' cellspacing='0' width='100%' class='GridView'>
                                <colgroup>
                                    <col style='width:30%;' />
                                    <col />
                                </colgroup>
                                {0}
                            </table>";

        if (customer.IsBangladeshi)
        {
            StringBuilder sbBangladeshi = new StringBuilder();
            sbBangladeshi.AppendFormat("<tr><td class='bold'>Country:</td> <td>{0}</td></tr>", "Bangladesh");
            sbBangladeshi.AppendFormat("<tr><td class='bold'>City:</td> <td>{0}</td></tr>", cities.Name);
            sbBangladeshi.AppendFormat("<tr><td class='bold'>Preferred Area:</td> <td>{0}</td></tr>", commaSeparatedZones);
            sbBangladeshi.AppendFormat("<tr><td class='bold'>Interested On:</td> <td>{0}</td></tr>", commaSeparatedName);
            sbBangladeshi.AppendFormat("<tr><td class='bold'>Interested In Loan/Financing:</td> <td>{0}</td></tr>", intrestedLoan);
            ltrInterestDetails.Text = String.Format(table2, sbBangladeshi.ToString());
        }
        else {  // Non Bangladeshi
            var country = context.Countries.FirstOrDefault(C => C.ID == customer.CountryID);
            StringBuilder sb2 = new StringBuilder();
            sb2.AppendFormat("<tr><td class='bold'>Country:</td> <td>{0}</td></tr>", country.Name.HtmlEncode());
            sb2.AppendFormat("<tr><td class='bold'>City:</td> <td>{0}</td></tr>", customer.CityName);
            sb2.AppendFormat("<tr><td class='bold'>Preferred Area:</td> <td>{0}</td></tr>", customer.PreferredZones);
            sb2.AppendFormat("<tr><td class='bold'>Interested On:</td> <td>{0}</td></tr>", commaSeparatedName);
            sb2.AppendFormat("<tr><td class='bold'>Interested In Loan/Financing:</td> <td>{0}</td></tr>", intrestedLoan);            
            ltrInterestDetails.Text = String.Format(table2, sb2.ToString());
        }

    }
}